package com.sc.nft.admin.rpc;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.sc.nft.admin.request.IdRequest;
import com.sc.nft.dao.EquityPropsDao;
import com.sc.nft.dto.*;
import com.sc.nft.entity.EquityProps;
import com.sc.nft.entity.UserPredictEcologyHatchRewardsRecords;
import com.sc.nft.entity.dto.MerchantAvailabilityListDTO;
import com.sc.nft.entity.vo.*;
import com.sc.nft.mapper.EquityPropsMapper;
import com.sc.nft.mapper.UserEquityPropsMapper;
import com.sc.nft.mapper.UserPredictEcologyHatchRewardsRecordsMapper;
import com.sc.nft.sup.PageRequest;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;

import static com.google.common.collect.Lists.*;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/rpc/equity")
@Api(tags = "虚拟道具RPC接口")
public class EquityPropsRpc {

    private final EquityPropsDao equityPropsDao;
    private final UserEquityPropsMapper userEquityPropsMapper;
    @Autowired
    private UserPredictEcologyHatchRewardsRecordsMapper userPredictEcologyHatchRewardsRecordsMapper;

    @PostMapping(value = "/simpleList", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "虚拟道具列表", notes = "虚拟道具列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiIgnore
    public PageResult<SimpleEquityPropsDTO> simpleList(@RequestBody PageRequest request) {
        return PageResult.success(equityPropsDao.getDaoUseList(request.toMp()), SimpleEquityPropsDTO.class);
    }

    @PostMapping(value = "/detail", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "虚拟道具详情", notes = "虚拟道具详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiIgnore
    public Result<EquityPropsDTO> detail(@Valid @RequestBody IdRequest request) {
        EquityProps equityProps = equityPropsDao.getById(request.getId());
        return Result.success(equityProps, EquityPropsDTO.class);
    }

    @PostMapping(value = "/simpleMerchantAvailabilityList", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "虚拟道具商户可用列表", notes = "虚拟道具商户可用列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiIgnore
    public PageResult<MerchantAvailabilityListVO> simpleMerchantAvailabilityList(@RequestBody MerchantAvailabilityListDTO merchantAvailabilityListDTO) {
        return PageResult.success(equityPropsDao.simpleMerchantAvailabilityList(merchantAvailabilityListDTO), MerchantAvailabilityListVO.class);
    }


    @PostMapping(value = "/countWithDao", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "DAO的道具卡数量", notes = "DAO的道具卡数量", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiIgnore
    public Result<List<DaoEquityPropsCountDTO>> countWithDao(@Valid @RequestBody IdRequest request) {
         return Result.success(userEquityPropsMapper.countWithDao(request.getId()));
    }

    @PostMapping(value = "/daoList", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "品牌虚拟道具列表", notes = "品牌虚拟道具列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiIgnore
    public Result<List<DaoEquityPropsDTO>> daoList(@Valid @RequestBody IdRequest request) {
        List<EquityProps> equityProps = equityPropsDao.getWithDao(request.getId());
        return Result.successColl(equityProps, DaoEquityPropsDTO.class);
    }
    @PostMapping(value = "/daoEquityHold", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "dao的道具持仓", notes = "dao的道具持仓", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiIgnore
    public PageResult<DaoEquityPropsVO> daoEquityHold(@Valid @RequestBody DaoEquityFindDTO dto) {
        Page<DaoEquityPropsVO> daoEquityPropsVOPage = userEquityPropsMapper.daoEquityHoldDetail(dto.toMp(), dto.getDaoId(), dto.getEquityPropsId(), dto.getPhoneNo(), dto.getStartTime(), dto.getEndTime());
        for (DaoEquityPropsVO record : daoEquityPropsVOPage.getRecords()) {
            record.setGetTypeDesc(record.getGetType().getDescription());
        }
        return PageResult.success(daoEquityPropsVOPage);
    }



    @PostMapping(value = "/equityWaitProduce", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "未来7天待产出道具", notes = "未来7天待产出道具", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiIgnore
    public Result<List<HashMap<String,String>>> equityWaitProduce(@Valid @RequestBody IdRequest request) {
        List<WaitProduceEquityCountListVO> waitProduceEquityCountListVOS = userPredictEcologyHatchRewardsRecordsMapper.equityWaitProduce(request.getId());
        HashMap<String,HashMap<String,String>> maps = Maps.newHashMap();
        List<HashMap<String,String>> result = Lists.newArrayList();
        for (WaitProduceEquityCountListVO waitProduceEquityCountListVO : waitProduceEquityCountListVOS) {
            HashMap<String,String> map= maps.get(waitProduceEquityCountListVO.getFirstTitle());
            if(map==null){
                map = Maps.newHashMap();
                maps.put(waitProduceEquityCountListVO.getFirstTitle(),map);
                map.put("firstTitle",waitProduceEquityCountListVO.getFirstTitle());
                result.add(map);
            }
            map.put(DateUtil.format(waitProduceEquityCountListVO.getSettleDate(),"yyyy-MM-dd"),waitProduceEquityCountListVO.getCount().toString());
        }
        return Result.success(result);
    }


}
